﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;
public partial class admin_listNguoiTimNguon : System.Web.UI.UserControl
{

#region "Member variables"
    private int intUserID = 0;
    private int intModuleID = 0;
    bool boolRead = false;
    bool boolWrite = false;
    bool boolUpdate = false;
    bool boolDelete = false;
    DANguoiTimNguon objNguoiTimNguon;
    DANguoidung objNguoidung;
    DANguon objNguon;
#endregion

#region "Private methods"
    //Lay Nguon theo Nguoitimnguon
    private void loadNguon()
    {
        DataSet dsNguon = objNguon.LayDanhSachNguon();
        if (dsNguon.Tables[0].Rows != null && dsNguon.Tables[0].Rows.Count > 0)
        {
            this.ddlNguon.Items.Clear();
            foreach (DataRow dr in dsNguon.Tables[0].Rows)
            {
                ListItem liTemp = new ListItem();
                liTemp.Text = dr["Ten"].ToString();
                liTemp.Value = dr["ID"].ToString();
                this.ddlNguon.Items.Add(liTemp);
            }
        }
    }
    // Hien thi NguoiTimNguon tren datagrid
    private void loadNguoiTimNguon(int intNguonID)
    {       
        DataView dv = new DataView();
        DataSet dsNguoiTimNguon = objNguoiTimNguon.loadNguoiTimNguonbyNguon(intNguonID);
        if (ViewState["sortExpr"] != null)
        {
            dv = new DataView(dsNguoiTimNguon.Tables[0]);
            dv.Sort = (string)ViewState["sortExpr"];
        }
        else
        {
            dv = dsNguoiTimNguon.Tables[0].DefaultView;
        }
        this.ddlNguoiTimNguon.DataSource = dv;
        this.ddlNguoiTimNguon.DataBind(); 
    }
    // Xoa NguoiTimNguon
    private void DeleteNguoiTimNguon(object source, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        int intNguoiTimNguonid = Convert.ToInt32(ddlNguoiTimNguon.DataKeys[e.RowIndex].Value);
        int intChẹck = objNguoiTimNguon.removeNguoiTimNguon(intNguoiTimNguonid);
        if (intChẹck > 0)
        {
            this.lblMess.Text = "Xóa thành công bản ghi";
            int intNguonID = Convert.ToInt32(this.ddlNguon.SelectedValue);
            loadNguoiTimNguon(intNguonID);
        }
        else
        {
            this.lblMess.Text = "Lỗi trong quá trình xóa bản ghi!";            
        }
    }
    //check validation
    private bool checkValidation()
    {
        string strFullName = this.txtHoTen.Text.Trim();
        if (strFullName.Equals(""))
        {
            this.ctvFullName.IsValid = false;
            return false;
        }

        else
        {
            return true;
        }
    }
    // Tao Address
    private void CreateNguoiTimNguon()
    {
        if (Page.IsValid)
        {
            int intNguonID = Convert.ToInt32(this.ddlNguon.SelectedValue);
            if (intNguonID > 0)
            {
                if (checkValidation())
                {
                    string strHoTen = this.txtHoTen.Text.Trim();
                    string strDiaChi = this.txtDiaChi.Text.Trim();
                    string strDienThoai = this.txtDienThoai.Text.Trim();
                    string strEmail = this.txtEmail.Text.Trim();
                    string strDiDong = this.txtDiDong.Text.Trim();
                    string strFax = this.txtFax.Text.Trim();
                    string strGhiChu = this.txtGhiChu.Text.Trim();
                    bool Sex = Convert.ToBoolean(this.ddlSex.SelectedValue);
                    int intChẹck = objNguoiTimNguon.insertNguoiTimNguon(strHoTen, strDiaChi, strDienThoai, strEmail,
                                            strDiDong, strFax, strGhiChu, Sex, intNguonID);
                    if (intChẹck > 0)
                    {
                        this.lblMess.Text = "Thêm bản ghi thành công";
                        resetFrom();
                        loadNguoiTimNguon(intNguonID);
                    }
                    else if (intChẹck == 0)
                    {
                        this.lblMess.Text = "Bản ghi này đã có trong dữ liệu!";
                    }
                    else
                    {
                        this.lblMess.Text = "Lỗi trong quá trình thêm mới bản ghi!";
                    }
                }
            }
        }
    }
    //Reset text cua from    
    private void resetFrom()
    {
        this.txtDiaChi.Text = "";
        this.txtDiDong.Text = "";
        this.txtEmail.Text = "";
        this.txtFax.Text = "";
        this.txtGhiChu.Text = "";
        this.txtEmail.Text = "";
        this.txtDienThoai.Text = "";
        this.txtHoTen.Text = "";
    }
    private void LoadUpdateID(object source, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        int intID = Convert.ToInt32(ddlNguoiTimNguon.DataKeys[e.NewEditIndex].Value);
        DataRow drNguoiTimNguon = objNguoiTimNguon.loadNguoiTimNguonID(intID);
        this.hdfNguoiTimNguonID.Value = intID.ToString();
        int intUser = Convert.ToInt32(drNguoiTimNguon["ID"].ToString().Trim());
        this.txtDiaChi.Text = drNguoiTimNguon["DiaChi"].ToString().Trim();
        this.txtDiDong.Text = drNguoiTimNguon["DiDong"].ToString().Trim();
        this.txtDienThoai.Text = drNguoiTimNguon["DienThoai"].ToString().Trim();
        this.txtEmail.Text = drNguoiTimNguon["Email"].ToString().Trim();
        this.txtFax.Text = drNguoiTimNguon["Fax"].ToString().Trim();
        this.txtGhiChu.Text = drNguoiTimNguon["GhiChu"].ToString().Trim();
        this.txtHoTen.Text = drNguoiTimNguon["HoTen"].ToString().Trim();
        bool strSex = Convert.ToBoolean(drNguoiTimNguon["GioiTinh"].ToString());
        this.ddlSex.Items.Clear();
        this.ddlSex.Items.Add(new ListItem("Nam", "True"));
        this.ddlSex.Items.Add(new ListItem("Nữ", "False"));

        if (strSex)
        {
            this.ddlSex.Items[0].Selected = true;
        }
        else
        {
            this.ddlSex.Items[1].Selected = true;
        }
    }
    // Sua Address
    private void UpdateNguoiTimNguon()
    {
        int intNguonID = Convert.ToInt32(this.ddlNguon.SelectedValue);
        if (intNguonID > 0)
        {
            if (checkValidation())
            {

                int intid = Convert.ToInt32(this.hdfNguoiTimNguonID.Value);
                string strHoTen = this.txtHoTen.Text.Trim();
                string strDiaChi = this.txtDiaChi.Text.Trim();
                string strDienThoai = this.txtDienThoai.Text.Trim();
                string strEmail = this.txtEmail.Text.Trim();
                string strDiDong = this.txtDiDong.Text.Trim();
                string strFax = this.txtFax.Text.Trim();
                string strGhiChu = this.txtGhiChu.Text.Trim();
                bool Sex = Convert.ToBoolean(this.ddlSex.SelectedValue);
                int intChẹck = objNguoiTimNguon.updateNguoiTimNguon(intid, strHoTen, strDiaChi, strDienThoai, strEmail,
                                        strDiDong, strFax, strGhiChu, Sex, intNguonID);
                if (intChẹck > 0)
                {
                    this.lblMess.Text = "Sửa thành công bản ghi";
                    this.btnUpdate.Visible = false;
                    resetFrom();
                    loadNguoiTimNguon(intNguonID);
                }
                else if (intChẹck == 0)
                {
                    this.lblMess.Text = "Bản ghi này đã có trong dữ liệu!";
                }
                else
                {
                    this.lblMess.Text = "Lỗi trong quá trình thêm mới bản ghi!";
                }
            }
        }
    }
#endregion
#region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion     
#region "Events handle"
    //Check quyen han su dung
    private void CheckUserModule(int UserID, int ModuleID)
    {
        //objUser.CheckUserModule(UserID, ModuleID, ref boolRead, ref boolWrite
        //    , ref boolUpdate, ref boolDelete);
        //if (!boolRead)
        //{
        //    this.btnInsert.Visible = false;
        //    this.ddlNguoiTimNguon.Visible = false;
        //}
        //if (!boolWrite)
        //{
        //    this.btnInsert.Visible = false;
        //}
        //if (!boolUpdate)
        //{
        //    this.ddlNguoiTimNguon.Columns[3].Visible = false;
        //    this.ddlNguoiTimNguon.Columns[5].Visible = false;
        //    this.btnRemove.Visible = false;
        //}
        //if (!boolDelete)
        //{
        //    this.ddlNguoiTimNguon.Columns[4].Visible = false;
        //    this.ddlNguoiTimNguon.Visible = false;
        //    this.ddlNguoiTimNguon.Visible = true;
        //}
        //else
        //{
        //    this.ddlNguoiTimNguon.Visible = true;
        //}
    }
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objNguoiTimNguon = new DANguoiTimNguon(sqlConnec);
        objNguoidung = new DANguoidung(sqlConnec);
        objNguon = new DANguon(sqlConnec);
    }
    //Main load
    protected void Page_Load(object sender, EventArgs e)
    {
            loadDataAccess();
            if (!Page.IsPostBack)
            {
                loadNguon();
                if (this.ddlNguon.Items.Count > 0)
                {
                    int NguonID = Convert.ToInt32(this.ddlNguon.SelectedValue);
                    loadNguoiTimNguon(NguonID);
                }
                this.btnUpdate.Visible = false;
            }
    }
 
    //Su kien bin from NguoiTimNguon
    protected void ddlNguoiTimNguon_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
                e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
                
                // Modify
                ImageButton lbtDeleteTemp = ((ImageButton)e.Row.FindControl("lbtDelete"));
                if (lbtDeleteTemp != null)
                {
                    string strDelete = "Bạn chắc chắn muốn xóa OK , Bạn hủy lệnh CANCEL ";
                    lbtDeleteTemp.Attributes.Add("Onclick", "javascript:if(confirm('" + strDelete + "')) {return true} else {return false};");
                }               
            }
        }

    }

    //Su kien xoa NguoiTimNguon
    protected void ddlNguoiTimNguon_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        this.lblMess.Text = "";
        DeleteNguoiTimNguon(sender, e);
    }
    
    protected void ddlNguoiTimNguon_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.ddlNguoiTimNguon.EditIndex = -1;
        this.ddlNguoiTimNguon.PageIndex = e.NewPageIndex;
        int NguonID = Convert.ToInt32(this.ddlNguon.SelectedValue);
        this.loadNguoiTimNguon(NguonID);
    }
    //Su kien edit form cua FileVideo
    protected void ddlNguoiTimNguon_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.btnUpdate.Visible = true;
        LoadUpdateID(sender, e);
        this.lblMess.Text = "";
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        this.lblMess.Text = "";
        UpdateNguoiTimNguon();
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        resetFrom();
        this.lblMess.Text = "";
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        this.lblMess.Text = "";
        CreateNguoiTimNguon();
    }
    protected void ddlNguoiTimNguon_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortOrder"] == null)
        {
            ViewState["SortOrder"] = " ASC";
        }
        else if (ViewState["SortOrder"].ToString() == " ASC")
        {
            ViewState["SortOrder"] = " DESC";
        }
        else
        {
            ViewState["SortOrder"] = " ASC";
        }
        ViewState["sortExpr"] = e.SortExpression + ViewState["SortOrder"];
        int NguonID = Convert.ToInt32(this.ddlNguon.SelectedValue);
        loadNguoiTimNguon(NguonID);
    }
    protected void ddlNguon_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.lblMess.Text = "";
        int NguonID = Convert.ToInt32(this.ddlNguon.SelectedValue);
        loadNguoiTimNguon(NguonID);

    }
#endregion

   
}
